﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace 址
{
    class Program
    {
        static void Main(string[] args)
        {
            int recvLengh;
            byte[] data = new byte[1024];
            IPEndPoint iend = new IPEndPoint(IPAddress.Any,9999);
            Socket newsock = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
            newsock.Bind(iend);
            newsock.Listen(10);
            Console.WriteLine("waiting for a clicent");
            Socket client = newsock.Accept();
            IPEndPoint clientAdd = (IPEndPoint)client.RemoteEndPoint;
            Console.WriteLine("连接:"+clientAdd.Address+"端口"+clientAdd.Port);
            string welcome = "welcome here!";
            data = Encoding.ASCII.GetBytes(welcome);
            client.Send(data,data.Length,SocketFlags.None);
            while(true)
            {data=new byte[1024];
            recvLengh = client.Receive(data);
            if (recvLengh == 0)
                break;
            Console.WriteLine(Encoding.ASCII.GetString(data,0,recvLengh));
            client.Send(data,recvLengh,SocketFlags.None);
            }
            Console.WriteLine("断开连接"+clientAdd.Address);
            client.Close();
            newsock.Close();
        }
    }
}
